﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 二圆判定
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Point O1 = new Point();
            Point O2 = new Point();
            char[] speraters = new char[] { ',', '，', '(', ')', '（', '）' };
            string[] PointA = txtO1.Text.Split(speraters, StringSplitOptions.RemoveEmptyEntries);
            O1 = new Point(int.Parse(PointA[0]), int.Parse(PointA[1]));
            string[] PointB = txtO2.Text.Split(speraters, StringSplitOptions.RemoveEmptyEntries);
            O2 = new Point(int.Parse(PointB[0]), int.Parse(PointB[1]));

            double d = Math.Sqrt((O1.X - O2.X) * (O1.X - O2.X) + (O1.Y - O2.Y) * (O1.Y - O2.Y));
            double r1 = double.Parse(txtR1.Text);
            double r2 = double.Parse(txtR2.Text);
            if( d > r1 + r2)
            {
                lblResult.Text = "相离";
            }else if(d == r1 + r2) {
                lblResult.Text = "相切";
            }else
            {
                double rb = 0;double rl = 0;
                if(r1 == r2)
                {
                    rb = r1;
                    rl = r2;
                }
                else
                {
                    rb = r1 > r2 ? r1 : r2;
                    rl = r1 > r2 ? r2 : r1;
                }
                if(d == rb - rl)
                {
                    lblResult.Text = "内切";
                }else if(d < rb - rl)
                {
                    lblResult.Text = "内含";
                }
                else
                {
                    double[] nums = GetPoints(O1, O2, r1, r2);
                    double x1 = nums[0];double y1 = nums[1];double x2 = nums[2];double y2 = nums[3];
                    double h = Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
                    double dl = Math.Sqrt(rl * rl - Math.Pow(h / 2, 2)); 
                    double db = Math.Sqrt(r2 * r2 - Math.Pow(h / 2, 2)); 
                    if(d < dl + db)
                    {
                        double a1 = Math.Atan2(h / 2, dl);
                        double a2 = Math.Atan2(h / 2, db);
                        double s = Math.Round((Math.PI - a1) * r1 * r1 + h * dl / 2 + a2 * rb * rb - h * db / 2, 2);
                        lblResult.Text = $"相交，相交面积为：{s}";
                    }
                    else
                    {
                        double a1 = Math.Atan2(h / 2, dl);
                        double a2 = Math.Atan2(h / 2, db);
                        double s = Math.Round(a1 * rl * rl + a2 * rb * rb - h * d / 2, 2);
                        lblResult.Text = $"相交，相交面积为：{s}";
                    }

                }
            }
        }

        private double[] GetPoints(Point o1,Point o2,double r1,double r2)
        {
            double[] num = new double[4];
            double x1 = o1.X; double y1 = o1.Y; double x2 = o2.X; double y2 = o2.Y;
            if (y1 != y2)
            {
                double b = (r1 * r1 - r2 * r2 + y2 * y2 - y1 * y1 + x2 * x2 - x1 * x1) / (2 * y2 - 2 * y1);
                double k = (2 * x1 - 2 * x2) / (2 * y2 - 2 * y1);

                double a = k * k + 1;
                double b2 = 2 * k * (b - y1) - 2 * x1;
                double c = x1 * x1 + y1 * y1 + b * b - 2 * b * y1 - r1 * r1;
                double dart = Math.Sqrt(b2 * b2 - 4 * a * c);

                num[0] = (-b2 + dart) / (2 * a);
                num[2] = (-b2 - dart) / (2 * a);
                num[1] = k * num[0] + b;
                num[3] = k * num[2] + b;
            }
            else
            {
                double x = (r1 * r1 - r2 * r2 + x2 * x2 - x1 * x1) / (2 * (x2 - x1));
                double dart = Math.Sqrt(r1 * r1 - Math.Pow((x - x1), 2));
                num[0] = x;
                num[1] = y1 + dart;
                num[2] = x;
                num[3] = y1 - dart;
            }
            return num;
        }
    }
}
